<?php

namespace App\Services\Draw\DrawObject;


use App\Enums\AppJumpEnum;
use App\Exceptions\BasicException;
use App\Jobs\QiPao\Draw\PetDrawStatisticsJob;
use App\Models\MainDB\Gift;
use App\Models\MainDB\GiftBackpack;
use App\Repositories\GiftRepository;
use App\Repositories\LogRepository;
use App\Repositories\Pet\PetPropsRepository;
use App\Services\Draw\DrawConst\CacheKey;
use App\Services\Draw\Enum\DrawEnum;
use App\Services\Draw\PoolGroupObject\PetIndependentPoolGroup;
use App\Services\Draw\PoolGroupObject\PetPublicPoolGroup;
use App\Services\Tools\PushChatService;
use App\Services\Tools\PushService;
use App\Services\Tools\RedisService;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Enums\RedisKeyEnum;

/**
 * 金耀魔盒抽奖实体
 * @author wrh
 */
class Pet extends Draw
{
    protected $isDrawCriticalGift = true;
    protected $cachePrefix = 'pet:';
    protected $balanceNotEnoughMsg = '魔盒数量不足';
    protected $drawLockKey = "draw:pet:lock_";
    protected $isDrawAccumulate = false; //是否做抽奖计数累计逻辑
    protected $privateKey = 'pet';

    public function __construct()
    {
        parent::__construct();
        $this->redisObject = app('redis')->connection('game');
        $this->poolConfigModel = app('model_lottery_pet_pool_config');
        $this->poolUserModel = app('model_lottery_pet_pool_user');
        $this->poolModel = app('model_lottery_pet_pool');
        $this->poolPrizeModel = app('model_lottery_pet_pool_prize');
        $this->poolGroupModel = app('model_lottery_pet_pool_group');
        $this->giftTemplateModel = app('model_lottery_pet_gift_template');
        $this->poolTemplateModel = app('model_lottery_pet_pool_template');
        $this->poolDrawModel = app('model_lottery_pet_pool_draw');
        $this->poolDrawRecordModel = app('model_lottery_pet_pool_draw_record');
        $this->levelModel = app('model_lottery_pet_level');
        $this->poolIndependentModel = app('model_lottery_pet_pool_independent');
        $this->poolIndependentPrizeModel = app('model_lottery_pet_pool_independent_prize');
        $this->criticalDrawRecordModel = app('model_lottery_pet_critical_draw_record');
        $this->appointGiftModel = app('model_lottery_pet_appoint_gift');

        $this->checkRecordModel = app('model_draw_check_record');
        $this->addPoolAccountRecordModel = app('model_draw_add_pool_account_record');

        $this->addPoolRepo = app('repo_add_pool');

        $this->independentPoolGroup = new PetIndependentPoolGroup($this->redisObject);
        $this->publicPoolGroup = new PetPublicPoolGroup($this->redisObject);
    }

    public function isNovice($user, $poolConfig)
    {
        return $user->input_week <= $poolConfig->novice_coin;
    }

    // 添加抽奖记录
    public function addDrawRecord($user, $poolGroup, array $drawIOPut, Collection $gifts, $criticalGift, array $drawPrizes, int $drawCount, int $drawPrizesCount)
    {
        //并发处理
        if ($poolGroup->group_id == 0) {
            $poolGroup->group_id = key($drawPrizes);
        }


        $drawData = [
            "user_id" => $user->user_id,
            "group_id" => $poolGroup->group_id,
            "tag" => $poolGroup->tag,
            "count" => $drawCount,
            "gift_count" => $drawPrizesCount,
            "input" => $drawIOPut['input'],
            "output" => $drawIOPut['output'],
        ];

        $poolDrawId = $this->poolDrawModel->createDataGetId($drawData);
        if (empty($poolDrawId)) {
            throw new BasicException(-1, '抽奖记录保存错误');
        }
        $drawPrizesRecord = [];
        foreach ($drawPrizes as $groupId => $groupPrize) {
            foreach ($groupPrize as $prizeId => $prizeCount) {
                $gift = $gifts->where('gift_template_id', $prizeId)->first();
                $drawPrizesRecord[] = ['user_id' => $user->user_id, 'draw_id' => $poolDrawId, 'group_id' => $groupId,
                    'gift_id' => $gift->id, 'gift_price' => $gift->price, 'count' => $prizeCount, 'gift_type' => $gift->type];
            }
        }
        if (!empty($criticalGift)) {
            $drawPrizesRecord[] = ['user_id' => $user->user_id, 'draw_id' => $poolDrawId, 'group_id' => 0,
                'gift_id' => $criticalGift['id'], 'gift_price' => 0, 'count' => $criticalGift['count'], 'gift_type' => DrawEnum::PROP_GIFT_TYPE];


        }

        $this->poolDrawRecordModel->insert($drawPrizesRecord);
        return $drawData;
    }

    public function staticsSeasonData($poolConfig, $redisObject, $prop, $user, $drawCount)
    {
        try {
            //累计赛季值
            $propGiftCount = $prop->count;
            if ($propGiftCount <= 0) {
                return null;
            }
            //赛季值达到
            $isSendSeasonGift = false;
            $seasonOriginSeq = 20220730000001;
            $nextSeasonPropCount = 0;
            $seasonSeq = $redisObject->get($this->cachePrefix . "seasonSeq") ?? 0;
            $luckyValue = 0;
            if ($redisObject->set($this->cachePrefix . "luckyDrawLock", 1, 'ex', 5, 'nx')) {
                if (($luckyValue = $redisObject->incrby($this->cachePrefix . "seasonValue", $propGiftCount)) >= $poolConfig['lucky_count']) {
                    //进入下个赛季
                    $seasonSeq = $redisObject->incrby($this->cachePrefix . "seasonSeq", 1);
                    //幸运值清零
                    $nextSeasonPropCount = $redisObject->incrby($this->cachePrefix . "seasonValue", -$poolConfig['lucky_count']);
                    $luckyValue = $luckyValue - $poolConfig['lucky_count'];
                    $isSendSeasonGift = true;
                    app('events')->listen('draw.redisJobRollback', function () use ($propGiftCount, $poolConfig, $redisObject) {
                        $redisObject->incrby($this->cachePrefix . "seasonValue", $poolConfig['lucky_count'] - $propGiftCount);
                    });
                } else {
                    $redisObject->del($this->cachePrefix . "luckyDrawLock");
                    app('events')->listen('draw.redisJobRollback', function () use ($propGiftCount, $poolConfig, $redisObject) {
                        $redisObject->incrby($this->cachePrefix . "seasonValue", -$propGiftCount);
                    });
                }
            } else {
                $luckyValue = $redisObject->incrby($this->cachePrefix . "seasonValue", $propGiftCount);
                app('events')->listen('draw.redisJobRollback', function () use ($propGiftCount, $poolConfig, $redisObject) {
                    $redisObject->incrby($this->cachePrefix . "seasonValue", -$propGiftCount);
                });
            }

            (new PushService())->petLuckyValuePush($luckyValue > $poolConfig->lucky_count ? ($luckyValue - $poolConfig->lucky_count) : $luckyValue, $poolConfig->lucky_count);

            $season = $seasonOriginSeq + $seasonSeq;
            //统计用户赛季道具信息
            $seasonAddPropGiftCount = $propGiftCount;
            $userSeasonRecord = app('model_lottery_pet_user_season_record')->firstOrCreate([
                "user_id" => $user->user_id,
                "season" => $season,
            ]);
            if ($isSendSeasonGift) {
                //更新上一季的道具值
                $lastSeason = app('model_lottery_pet_user_season_record')->firstOrCreate([
                    "user_id" => $user->user_id,
                    "season" => $season - 1,
                ]);
                $lastSeason->draw_count += $drawCount;
                $lastSeason->prop_count += $propGiftCount - $nextSeasonPropCount;
                $lastSeason->save();
                $seasonAddPropGiftCount = $nextSeasonPropCount;
            } else {
                $userSeasonRecord = app('model_lottery_pet_user_season_record')->firstOrCreate([
                    "user_id" => $user->user_id,
                    "season" => $season,
                ]);
                $userSeasonRecord->draw_count += $drawCount;
            }
            if ($seasonAddPropGiftCount > 0) {
                if (empty($userSeasonRecord)) {
                    $userSeasonRecord = app('model_lottery_pet_user_season_record')->firstOrCreate([
                        "user_id" => $user->user_id,
                        "season" => $season,
                    ]);
                }
                $userSeasonRecord->prop_count += $seasonAddPropGiftCount;
                $userSeasonRecord->save();
            }

            //统计赛季道具
            if ($isSendSeasonGift || $seasonSeq == 0) {
                //新赛季
                $seasonStatistics = app('model_lottery_pet_season_statistics')->firstOrCreate(['season' => $season]);
            }

            $redisObject->setbit($this->cachePrefix . "season:userStatistics" . "_" . $season, $user->user_id, 1);
            $userCount = $redisObject->bitcount($this->cachePrefix . "season:userStatistics" . "_" . $season);
            if ($isSendSeasonGift) {
                //更新上一季的道具值
                $lastSeasonUserCount = $redisObject->bitcount($this->cachePrefix . "season:userStatistics" . "_" . ($season - 1));
                app('model_lottery_pet_season_statistics')->updateData(['season' => $season - 1], ['user_count' => $lastSeasonUserCount,
                    'prop_count' => DB::raw('prop_count+' . ($propGiftCount - $nextSeasonPropCount))]);
            }
            app('model_lottery_pet_season_statistics')->updateData(['season' => $season], ['user_count' => $userCount,
                'prop_count' => DB::raw('prop_count+' . $seasonAddPropGiftCount)]);
            //排行榜道具发放
            if ($isSendSeasonGift) {
                $prop = $this->giftTemplateModel->getPropInfo($poolConfig['lucky_gift_template_id']);
                $this->settleLastSeason($season, $prop);
            }
        } catch (\Exception $e) {
            Log::error('赛季道具发送异常' . $e->getMessage());
            throw $e;

        }

    }

    public function settleLastSeason($season, $props)
    {

        //查询上个赛季排行榜用户
        $lastSeason = app('model_lottery_pet_season_statistics')->one([['season', '<', $season], 'present_prop_count' => 0]);
        $seasonRankList = app('model_lottery_pet_user_season_record')->list(['season' => $lastSeason['season']], ['user_id', 'prop_count'], 'prop_count');
        if (empty($seasonRankList)) {
            Log::info('未找到上赛季数据' . $season);
            return null;
        }
        $seasonPresentPropCount = 0;
        $presentPropGiftData = [];
        $presentPropCount = 0;
        foreach ($seasonRankList as $key => $data) {
            $rank = $key + 1;
            if ($key >= 3) {
                $presentPropGiftData[] = [
                    'id' => $props->id,
                    'user_id' => $data['user_id'],
                    'present_prop_count' => 20,
                    'count' => 20
                ];
                $seasonPresentPropCount += 20;
                $prizeMessage = "共计奖励20枚{$props->name}";
            } else {
                $presentPropGiftData[] = [
                    'id' => $props->id,
                    'user_id' => $data['user_id'],
                    'present_prop_count' => $data['prop_count'] * (3 - $key),
                    'count' => $data['prop_count'] * (3 - $key)
                ];
                $seasonPresentPropCount += $data['prop_count'] * (3 - $key);
                $presentPropCount = $data['prop_count'] * (3 - $key);
                switch ($key) {
                    case 0:
                        $prizeMessage = "{$props->name}量额外三倍，共计奖励{$presentPropCount}枚{$props->name}";
                        break;
                    case 1:
                        $prizeMessage = "{$props->name}数量额外二倍，共计奖励{$presentPropCount}枚{$props->name}";
                        break;
                    case 2:
                        $prizeMessage = "{$props->name}数量额外一倍，共计奖励{$presentPropCount}枚{$props->name}";
                        break;
                }
            }

            app('model_user_news')->sendUserNews($data['user_id'], '活动通知',
                "恭喜您在【萌兽盲盒】第【{$lastSeason->season}】赛季中获得第{$rank}名，" . $prizeMessage . "，请前往【绿洲集市】进行查看，祝心愿达成！",
                0, 0, 0, AppJumpEnum::SYSTEM_MESSAGE_SOURCE);


        }
        DB::connection('mysql_game')->beginTransaction();
        try {
            //统计赛季赠送道具
            app('model_lottery_pet_season_statistics')->updateData(['season' => $lastSeason['season']], ['season_end_time' => Carbon::now(), 'present_prop_count' => $seasonPresentPropCount]);
            //统计赛季用户赠送道具
            app('model_lottery_pet_user_season_record')->batchUpdatePresentProps($lastSeason['season'], $presentPropGiftData);
            //发送道具到用户背包
            PetPropsRepository::getInstance()->batchAddUserProps($presentPropGiftData);
            DB::connection('mysql_game')->commit();
        } catch (\Exception $e) {
            DB::connection('mysql_game')->rollBack();
            throw $e;
        }
    }

    public function getDrawJob($poolGroup, $user, $jobData)
    {
        return new PetDrawStatisticsJob($poolGroup, $user, $jobData, $this->cachePrefix);

    }

//    public function getPoolTemplateGiftList($poolGroup)
//    {
//        $giftTemplateList = $this->poolTemplateModel->getGiftTemplateList(['group_id' => $poolGroup->group_id], ['gift_template_id', 'count', 'count as template_count', 'type', 'gift_id']);
//        $propGiftIds      = $giftTemplateList->where('type', DrawEnum::PROP_GIFT_TYPE)->pluck('gift_id');
//        $giftIds          = $giftTemplateList->where('type', DrawEnum::NORMAL_GIFT_TYPE)->pluck('gift_id');
//
//        $propGiftListWithId = [];
//        if (!empty($propGiftIds)) {
//            //查询道具价格
//            $propGiftList = app('model_props')->select('id', 'price')->whereIn('id', $propGiftIds)->where('is_delete', 0)->get();
//            if ($propGiftList->isNotEmpty()) {
//                $propGiftListWithId = $propGiftList->KeyBy('id');
//            }
//        }
//
//        $gifts       = (new GiftRepository())->giftsByIdsWithField($giftIds, ['id', 'price']); // 抽中礼物信息
//        $giftsWithId = $gifts->keyBy('id');
//        foreach ($giftTemplateList as &$template) {
//            if ($template['type'] == DrawEnum::PROP_GIFT_TYPE) {
//                //$template['price'] = $giftsWithId[$template['gift_id']]['price'];
//                $template['price'] = isset($propGiftListWithId[$template['gift_id']]) ? $propGiftListWithId[$template['gift_id']]['price'] : 0;
//
//            } else {
//                $template['price'] = $giftsWithId[$template['gift_id']]['price'];
//            }
//
//            $template['group_id'] = $poolGroup->group_id;
//        }
//        return $giftTemplateList;
//    }

    public function drawCriticalGift($user, int $drawCount, $poolConfig, $poolGroup)
    {
        //累计抽奖计数 判断是否可以出暴击礼物
        $redisLimitKey = $this->cachePrefix . CacheKey::POOL_CRITICAL_PRIZE_LIMIT . "_" . $poolGroup->pool_id;
        $prop = [];
        $poolModel = $this->poolModel;
        $poolId = $poolGroup->pool_id;
        if ($poolGroup->prop_count + $drawCount > $poolGroup->prop_random && $this->redisObject->setnx($redisLimitKey, 1)) {
            $this->redisObject->expire($redisLimitKey, 3);
            $criticalGiftStepSection = json_decode($poolGroup->prop_step_section, true);
            $criticalGiftCountSection = json_decode($poolGroup->prop_count_section, true);
            $poolModel->updateData(['id' => $poolId], ['prop_count' => 0, 'prop_random' => mt_rand($criticalGiftStepSection[0], $criticalGiftStepSection[1])]);


            $criticalGiftCount = mt_rand($criticalGiftCountSection[0], $criticalGiftCountSection[1]);
            $prop = $this->giftTemplateModel->getPropInfo($poolConfig->lucky_gift_template_id);
            if (empty($prop)) {
                return [];
            }

            $prop->count = $criticalGiftCount;
            //统计赛季幸运值
            $this->staticsSeasonData($poolConfig, $this->redisObject, $prop, $user, $drawCount);
        } else {
            $poolModel->updateData(['id' => $poolId], ['prop_count' => DB::raw('prop_count +' . $drawCount)]);
        }

        return $prop;
//
//
//        if (empty($criticalGiftSection)) {
//            return [];
//        }
//
//
//
//        //幸运值达到 给用户送幸运礼物
//        if ($this->redisObject->set($this->cachePrefix . "luckyDrawLock", 1, 'ex', 5, 'nx')) {
//            if ($this->redisObject->incrby($this->cachePrefix . "luckyValue", $criticalGiftCount) >= $poolConfig['lucky_count']) {
//                //幸运值清零
//                $this->redisObject->incrby($this->cachePrefix . "luckyValue", -$poolConfig['lucky_count']);
//
//                $luckyGiftTemplate = $this->giftTemplateModel->one(['id' => $poolConfig['lucky_gift_template_id']]);
//                $luckyGift         = (new GiftRepository())->one(['id' => $luckyGiftTemplate['gift_id'], 'is_delete' => 0]);
//                $luckyGift->count  = 1;
//                $this->criticalDrawRecordModel->insert(['user_id' => $user->user_id, 'gift_id' => $luckyGift->id, 'gift_price' => $luckyGift->price, 'count' => $criticalGiftCount]);
//                app('events')->listen('draw.redisRollback', function () use ($criticalGiftCount, $poolConfig) {
//                    $this->redisObject->incrby($this->cachePrefix . "luckyValue", $poolConfig['lucky_count'] - $criticalGiftCount);
//                });
//                return $luckyGift;
//            } else {
//                $this->redisObject->del($this->cachePrefix . "luckyDrawLock");
//                app('events')->listen('draw.redisRollback', function () use ($criticalGiftCount, $poolConfig) {
//                    $this->redisObject->incrby($this->cachePrefix . "luckyValue", -$criticalGiftCount);
//                });
//            }
//        } else {
//            $this->redisObject->incrby($this->cachePrefix . "luckyValue", $criticalGiftCount);
//            app('events')->listen('draw.redisRollback', function () use ($criticalGiftCount, $poolConfig) {
//                $this->redisObject->incrby($this->cachePrefix . "luckyValue", -$criticalGiftCount);
//            });
//        }
//        return [];

    }

    public function getGiftList(array $drawPrizes)
    {
        //根据模板礼物ID获取礼物ID
        $templateGiftIds = [];
        $giftPrizes = [];
        foreach ($drawPrizes as $groupId => $groupPrize) {
            foreach ($groupPrize as $prizeId => $prizeCount) {
                $templateGiftIds[] = $prizeId;
                if (isset($giftPrizes[$prizeId])) {
                    $giftPrizes[$prizeId] += $prizeCount;
                } else {
                    $giftPrizes[$prizeId] = $prizeCount;
                }
            }
        }

        $templateGiftList = $this->giftTemplateModel->getGiftListByIds($templateGiftIds, ['id', 'type', 'gift_id']);
        $templateGiftWithId = $templateGiftList->where('type', DrawEnum::NORMAL_GIFT_TYPE)->KeyBy('gift_id');
        //$templatePropWithId = $templateGiftList->where('type', DrawEnum::PROP_GIFT_TYPE)->KeyBy('gift_id');
        $giftInfoList = (new GiftRepository())->giftsByIds($templateGiftWithId->keys()); // 抽中礼物信息

        foreach ($giftInfoList as $giftInfo) {
            $giftInfo->gift_template_id = $templateGiftWithId[$giftInfo['id']] ? $templateGiftWithId[$giftInfo['id']]['id'] : 0;
            $giftInfo->count = $giftPrizes[$giftInfo->gift_template_id] ?? 0;
            $giftInfo->type = DrawEnum::NORMAL_GIFT_TYPE;
        }

//        if (!empty($templatePropWithId)) {
//            //查询道具价格
//            $propGiftList = app('model_props')->select('id', 'name', 'prope_icon as picture', 'price')->whereIn('id', $templatePropWithId->keys())->where('is_delete', 0)->get();
//            if ($propGiftList->isNotEmpty()) {
//                $propGiftListWithId = $propGiftList->KeyBy('id');
//                foreach ($templatePropWithId as $templateId => $propGift) {
//                    $propGiftInfo                   = $propGiftListWithId[$propGift['gift_id']];
//                    $propGiftInfo->gift_template_id = $propGift->id;
//                    $propGiftInfo->count            = $giftPrizes[$propGift->id] ?? 0;
//                    $propGiftInfo->type             = DrawEnum::PROP_GIFT_TYPE;
//                    $giftInfoList[]                 = $propGiftInfo;
//                }
//            }
//        }

        return $giftInfoList;
    }

    public function getNotifyGiftList($count)
    {
        $cacheNotifyRecordList = $this->redisObject->get($this->cachePrefix . "notifyRecordList:" . $count);
        if (!empty($cacheNotifyRecordList)) {
            $notifyRecordList = json_decode($cacheNotifyRecordList, true);
        }
        if (empty($notifyRecordList)) {
            $notifyRecordList = parent::getNotifyGiftList($count);
            $this->redisObject->setex($this->cachePrefix . "notifyRecordList:" . $count, 60, json_encode($notifyRecordList));
        }

        return $notifyRecordList;
    }
//    public function push($user, array $notifyPushData, array $broadcastPushData, array $extraPushData)
//    {
//        try {
//            $userChatLabel = RedisService::getUserChatLabel($user->user_id);
    //$levelInfo = $this->levelModel->one(['level' => $user->level]);
//
//            foreach ($notifyPushData as $value) {
//                 (new PushChatService())->lotteryPet($userChatLabel, $value);
//            }
//            foreach ($notifyPushData as $value) {
//                (new PushChatService())->lotteryPetNew($userChatLabel, $value);
//            }
//            (new PushService())->lotteryPet($broadcastPushData, $userChatLabel);
//            } catch (\Exception $e) {
//                return false;
//            }
//                    return true;
//  }

//    public function push($user, array $notifyPushData, array $broadcastPushData, array $extraPushData)
//    {
//        try {
//            $uni_key = uniqidReal();
//            $getMillisecond = getMillisecond();
//            $PushService = new PushService();
//            $userChatLabel = RedisService::getUserChatLabel($user->user_id);
//            $ext_data = $userChatLabel;
//            //幸运票品
//            $notifyPush = [];
//            if(!empty($notifyPushData)) {
//                $txt = "<font color='#C5A1FF'>在金耀魔盒中获得了</font>";
//                foreach ($notifyPushData as $key => $value) {
//                    $txt .= '<font color="#FFC07D">价值' . $value->price . '</font>';
//                    $txt .= '<font color="#C5A1FF">金币的</font>';
//                    //$txt .= "<font color='#22C4F1'> </font>";
//                    $txt .= '<font color="#72D1E0">' . $value->name . ' x ' . $value->count . '.' . '</font>';
//                }
//                $ext_data['type'] = PushService::GAME_PUSH_STATUS;
//                $ext_data['action'] = 1;
//                $ext_data['show_area'] = [
//                    PushChatService::AREA_LUCKY,
//                    PushChatService::AREA_ALL,
//                ];
//                $ext_data['show_label_id'] = [
//                    PushChatService::AREA_LUCKY,
//                ];
//                $notifyPush = [
//                    'content' => $txt,
//                    'uni_key' => $uni_key,
//                    'extra' => $ext_data
//                ];
//            }
//            // 大飘屏
//            $broadcastPush = [];
//            if(!empty($broadcastPushData)) {
//                $txt = "<font color='#FD8469'>" . mb_substr($userChatLabel['nickname'], 0, 1) . "**</font><font color='#FFFFFF'> 获得了价值 </font>";
//                $txt_extra = [];
//                foreach ($broadcastPushData as $gift) {
//                    $txt_extra [] = [
//                        'text' => $txt . "<font color='#F783FF'>" . $gift->price . "金币的</font>" . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
//                        'picture' => $gift->picture,
//                    ];
//                }
//                $broadcastPush = [
//                    'uni_key' => $uni_key,
//                    'time' => $getMillisecond,
//                    'msg' => $txt_extra,
//                    'special' => RedisService::getBroadcastSpecial($user->user_id),
//                ];
//            }
//
//            $PushService->publicGamePush($notifyPush,$broadcastPush,[],[],$uni_key,$getMillisecond);
//
//        } catch (\Exception $e) {
//            return false;
//        }
//        return true;
//    }

    public function push($user, array $notifyPushData, array $broadcastPushData, array $extraPushData)
    {
        try {
            $userChatLabel = RedisService::getUserChatLabel($user->user_id);
            $levelInfo = $this->levelModel->one(['level' => $user->level]);
            $uni_key = uniqidReal();
            $getMillisecond = getMillisecond();
            $PushService = new PushService();
            // /幸运票品
            $ext_data = $userChatLabel;
            $extra_data = $userChatLabel;
            $notifyPush = [];
            if (!empty($notifyPushData)) {
                $txt = "<font color='#BDF1FF'>" . $levelInfo->name . " </font>";
                $txt .= "<font color='#3AA4E6'>" . $userChatLabel['nickname'] . " </font>";
                $txt .= "<font color='#BDFFD1'>在金耀魔盒中意外获得 </font>";
                foreach ($notifyPushData as $key => $value) {
                    $txt .= '<font color="#FFE6BC">' . $value->price . '</font>';
                    $txt .= '<font color="#BDFFD1"> 金币的</font>';
                    $txt .= '<font color="#3ADBE6">' . $value->name . ' x ' . $value->count . '.' . '</font>';
                }
                $txt .= '<font color="#BDFFD1"> 礼物，运气爆棚！</font>';
                $ext_data['type'] = PushService::GAME_PUSH_STATUS;
                $ext_data['action'] = 1;
                $ext_data['show_area'] = [
                    PushChatService::AREA_LUCKY,
                    PushChatService::AREA_ALL,
                ];
                $ext_data['show_label_id'] = [
                    PushChatService::AREA_LUCKY,
                ];
                $ext_data['bg_type'] = $this->privateKey;
                $notifyPush = [
                    'content' => $txt,
                    'uni_key' => $uni_key,
                    'extra' => $ext_data
                ];
            }
            $broadcastPush = [];
            if (!empty($broadcastPushData)) {
                $txt = "<font color='#FFFFFF'>" . $levelInfo->name . " </font><font color='#FFEB66'>" . $userChatLabel['nickname'] . "</font><font color='#FFFFFF'> 在金耀魔盒中意外获得 </font>";
                $txt_extra = [];
                foreach ($broadcastPushData as $gift) {
                    $txt_extra [] = [
                        'text' => $txt . "<font color='#FFEB66'>" . $gift->price . "金币的</font>" . "<font color='#73E8FF'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                        'picture' => $gift->picture,
                    ];

                }
                $txt_extra[count($txt_extra) - 1]['text'] .= "<font color='#FFFFFF'>，震摄八方！</font>";
                $broadcastPush = [
                    'uni_key' => $uni_key,
                    'time' => $getMillisecond,
                    'msg' => $txt_extra,
                    'special' => RedisService::getBroadcastSpecial($user->user_id),
                    'bg_type' => $this->privateKey,
                    'show_grade' => $this->showBroadcastGrade(),
                ];
            }

            $extraPush = [];
            if (!empty($extraPushData)) {
                $extra_txt = "<font color='#FFD769'>" . $levelInfo->name . " </font>";
                $extra_txt .= "<font color='#E68D3A'>" . $userChatLabel['nickname'] . " </font>";
                $extra_txt .= "<font color='#46E0FF'>在许愿中获得 </font>";
                foreach ($extraPushData as $key => $value) {
                    $extra_txt .= '<font color="#FFEB6A">' . $gift['price'] . '</font>';
                    $extra_txt .= '<font color="#46E0FF"> 金币的等级专属礼物 </font>';
                    $extra_txt .= '<font color="#E6573A">' . $gift['name'] . '.' . '</font>';
                }
                $extra_txt .= '<font color="#46E0FF"> ，吉人天相！</font>';
                $extra_data['type'] = PushService::GAME_PUSH_STATUS;
                $extra_data['action'] = 1;
                $extra_data['show_area'] = [
                    PushChatService::AREA_LUCKY,
                    PushChatService::AREA_ALL,
                ];
                $extra_data['show_label_id'] = [
                    PushChatService::AREA_LUCKY,
                ];
                $extraPush = [
                    'content' => $extra_txt,
                    'uni_key' => $uni_key,
                    'extra' => $extra_data
                ];
            }

            $PushService->publicGamePush($notifyPush, $broadcastPush, $extraPush, [], $uni_key, $getMillisecond);

        } catch (\Exception $e) {
            return false;
        }
        return true;
    }

    public function getUserQueuePool($userId, int $count)
    {
        return null;

    }

//    public function getUserPool()
//    {
//
//    }

//
//    /**
//     * @param int $userId
//     * @param int $count
//     */
//    public function swap(int $userId, int $count)
//    {
//
//    }


}
